package com.ruoyi.ihome.controller;

import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ihome.domain.SysIntegral;
import com.ruoyi.ihome.service.ISysIntegralService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 信誉积分Controller
 *
 * @author mage
 * @date 2024-08-16
 */
@RestController
@RequestMapping("/ihome/integral")
public class SysIntegralController extends BaseController {
    @Autowired
    private ISysIntegralService sysIntegralService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 查询信誉积分列表
     */
    @PreAuthorize("@ss.hasPermi('ihome:integral:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysIntegral sysIntegral) {
        startPage();
        List<SysIntegral> list = sysIntegralService.selectSysIntegralList(sysIntegral);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ihome:integral:list')")
    @GetMapping("/userIntegral")
    public TableDataInfo selectUserIntegral(SysIntegral sysIntegral) {
        startPage();
        List<SysIntegral> list = sysIntegralService.selectUserIntegral(sysIntegral);
        return getDataTable(list);
    }

    /**
     * 导出信誉积分列表
     */
    @PreAuthorize("@ss.hasPermi('ihome:integral:export')")
    @Log(title = "信誉积分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysIntegral sysIntegral) {
        List<SysIntegral> list = sysIntegralService.selectSysIntegralList(sysIntegral);
        ExcelUtil<SysIntegral> util = new ExcelUtil<SysIntegral>(SysIntegral.class);
        util.exportExcel(response, list, "信誉积分数据");
    }

    /**
     * 获取信誉积分详细信息
     */
    @PreAuthorize("@ss.hasPermi('ihome:integral:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysIntegralService.selectSysIntegralById(id));
    }


    @PreAuthorize("@ss.hasPermi('ihome:integral:query')")
    @GetMapping(value = "/user/{id}")
    public AjaxResult selectSysIntegralByUserId(@PathVariable("id") Long id) {
        return success(sysIntegralService.selectSysIntegralByUserId(id));
    }

    /**
     * 新增信誉积分
     */
    @PreAuthorize("@ss.hasPermi('ihome:integral:add')")
    @Log(title = "信誉积分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysIntegral sysIntegral) {
        return toAjax(sysIntegralService.insertSysIntegral(sysIntegral));
    }

    /**
     * 修改信誉积分
     */
    @PreAuthorize("@ss.hasPermi('ihome:integral:edit')")
    @Log(title = "信誉积分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysIntegral sysIntegral) {
        return toAjax(sysIntegralService.updateSysIntegral(sysIntegral));
    }

    /**
     * 删除信誉积分
     */
    @PreAuthorize("@ss.hasPermi('ihome:integral:remove')")
    @Log(title = "信誉积分", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysIntegralService.deleteSysIntegralByIds(ids));
    }

    @Scheduled(cron = "0 */30 * * * ?")
    public void updateIntegral(){
        Set<Object> range = redisTemplate.opsForZSet().range("sys_int:integral", 0, -1);
        for (Object userId: range) {
            SysIntegral integral = sysIntegralService.selectSysIntegralByUserId((Long) userId);
            SysIntegral sysIntegral = new SysIntegral();
            sysIntegral.setId(integral.getId());
            sysIntegral.setUserId((Long)userId);
            sysIntegral.setIntegral(redisTemplate.opsForZSet().score("sys_int:integral", userId));
            sysIntegralService.updateSysIntegral(sysIntegral);
        }
    }
}

